﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Devices.Sensors;
using System.ComponentModel;
namespace Phone7ActionPack.Controls
{
    public class AccelerometerControl : FrameworkElement, INotifyPropertyChanged
    {
        AccelerometerSensor sensor;

        private double x;
        public double X
        {
            get { return this.x; }
            protected set
            {
                if (this.x != value)
                {
                    this.x = value;
                    this.OnPropertyChanged("X");
                }
            }
        }

        private double y;
        public double Y
        {
            get { return this.y; }
            protected set
            {
                if (this.y != value)
                {
                    this.y = value;
                    this.OnPropertyChanged("Y");
                }
            }
        }

        private double z;
        public double Z
        {
            get { return this.z; }
            protected set
            {
                if (this.z != value)
                {
                    this.z = value;
                    this.OnPropertyChanged("Z");
                }
            }
        }


        public AccelerometerControl()
        {
            sensor = new AccelerometerSensor();
            sensor.ReadingChanged += new EventHandler<AccelerometerReadingAsyncEventArgs>(sensor_ReadingChanged);

        }

        private void sensor_ReadingChanged(object sender, AccelerometerReadingAsyncEventArgs e)
        {
            this.X = e.Value.Value.X;
            this.Y = e.Value.Value.Y;
            this.Z = e.Value.Value.Z;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
